<?php
class contentList extends CI_Model {
	
	public $contentType = '';
	public $headings;
	public $rows;
	public $fields;
	public $rKey = 'uid';
	public $customType = false;
	
	public $hasEdit   = true;
	public $hasDelete = true;
	
	public function setFields($array)
	{
		$this->fields = array_merge($array);
	}
	
	public function init( $name, $headings, $list )
	{
		$this->contentType = $name;
		
		$added = array();
		
		if ( $this->hasEdit ) { $added[] = 'edit'; }
		if ( $this->hasDelete ) { $added[] = 'delete'; }
		
		$this->headings = array_merge($this->fields, $added);
		$ucRkey = ucfirst($this->rKey);
		//Make sure that headings are all capitalized.
		foreach( $this->headings as $key=>$value ) {
			$this->headings[$key] = ucfirst($value);
			if ( $this->headings[$key] == $ucRkey ) {
				unset($this->headings[$key]);
			}
		}
		
		$this->rows = $list;
	}
	
	public function draw($loadContent = true)
	{
		$this->load->library('table');
		
		$rows = array();
		$rows[] = $this->headings;
		$rKey = $this->rKey;
		$ucRkey = ucfirst($rKey);
		
		foreach ( $this->rows as $key=>$value ) {
			$row = array();
			
			foreach ($value as $rowkey=>$val) {
				if ($rowkey != $this->rKey && $rowkey != $ucRkey && in_array($rowkey, $this->fields) ) {
					$row[] = $val;
				}
			}
			
			if ( $this->customType === true ) {
				if ( $this->hasEdit ) { $row[] = '<a href="' . admin::$adminURL . 'other/show/' . $this->contentType . '/edit/' . $value->$rKey . '/">Edit</a>'; }
				if ( $this->hasDelete ) { $row[] = '<a href="' . admin::$adminURL . 'other/show/' . $this->contentType . '/delete/' . $value->$rKey . '/">Delete</a>'; }
			} else {
				if ( $this->hasEdit ) { $row[] = '<a href="' . admin::$adminURL . 'edit/' . $this->contentType . '/' . $value->$rKey . '/">Edit</a>'; }
				if ( $this->hasDelete ) { $row[] = '<a href="' . admin::$adminURL . 'delete/' . $this->contentType . '/' . $value->$rKey . '/">Delete</a>'; }
			}
			
			$rows[] = $row;
			
		}
		
		$tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="4" cellspacing="0">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td class="info">',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td class="info">',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
		
        $this->table->set_template($tmpl);
		$rows = $this->table->generate($rows);
		
		if ( $loadContent === true ) {
			$this->load->View('admin/pageView',array('content'=>$rows));
		} else {
			return $rows;
		}
		
	}
	
}